package com.lvmou.index12306.framework.starter.bases.init;

import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 应用初始化后置处理器
 */
@RequiredArgsConstructor
public class ApplicationContentPostProcessor implements ApplicationListener {

    private final ApplicationContext applicationContext;

    /**
     * 执行标识 确保spring事件有且执行一次
     */
    private final AtomicBoolean executeOnlyOnce = new AtomicBoolean(false);
    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (!executeOnlyOnce.compareAndSet(false, true)){
            return;
        }
        applicationContext.publishEvent(new ApplicationInitializingEvent(this));
    }
}
